package com.xtxb.utils.log.impl.file;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 作者: xtxb-fedora
 * <p>
 * 日期: 2021年01月22日-下午5:23
 * <p>
 * 版权说明：xtxb
 * <p>
 *
 */
public class FileOutputQueue implements Runnable {
    private BlockingQueue<String> queue=null;
    private static FileOutputQueue instance=null;

    private FileOutputQueue(){
        queue=new LinkedBlockingQueue<>();
        Thread t=new Thread(new ThreadGroup("debug_log"),this,"Debug Log Out Queue Thread");
        t.setDaemon(true);
        t.start();
    }

    public synchronized  static  FileOutputQueue getInstance(){
        if(instance==null)
            instance=new FileOutputQueue();
        return instance;
    }

    public  void addMsg(String msg){
        try {
            queue.put(msg);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        FileOut out = new FileOut();
        while (true) {
            try {
                String msg=queue.take();
                out.getOut().write( msg+ "\n");
                out.getOut().flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
